package com.yc.spring.demo;

import com.yc.spring.demo.biz.UserBiz;
import com.yc.spring.demo.mapper.UserMapper;
import com.yc.spring.demo.web.UserController;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/7/31
 * Email haijunzhou@hnit.edu.cn
 */
@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:bean.xml")
public class SpringDemo {

    @Test
    public void test1(){
        ClassPathXmlApplicationContext cxt =
                new ClassPathXmlApplicationContext("bean.xml");
        System.out.println("***********************************");
        Person person = cxt.getBean(Person.class);
//        person.setName("上三");
        person.hello();

        Person person1 = (Person) cxt.getBean("person1");
        Person person2 = (Person) cxt.getBean("person2");

        person1.hello();
        person2.hello();

        System.out.println("##########################");
        Object person3 = cxt.getBean("person3");

        System.out.println("person3 = " + person3);

        Object person31 = cxt.getBean("person3");

        Object person32 = cxt.getBean("person3");

        Object person33 = cxt.getBean("person3");

        System.out.println(person3 == person31);
        System.out.println(person3 == person32);
        System.out.println(person3 == person33);
        cxt.close();

    }

    @Test
    public void test2(){
        AnnotationConfigApplicationContext cxt =
                new AnnotationConfigApplicationContext(AnnoConfig.class);
        System.out.println("***********************************");
        Person person = cxt.getBean(Person.class);
//        person.setName("上三");
        person.hello();

        Person person1 = (Person) cxt.getBean("person1");
        Person person2 = (Person) cxt.getBean("person2");

        person1.hello();
        person2.hello();

        System.out.println("##########################");
        Object person3 = cxt.getBean("person3");

        System.out.println("person3 = " + person3);

        Object person31 = cxt.getBean("person3");

        Object person32 = cxt.getBean("person3");

        Object person33 = cxt.getBean("person3");

        System.out.println(person3 == person31);
        System.out.println(person3 == person32);
        System.out.println(person3 == person33);
        cxt.close();
    }


    @Autowired   //自动装配注解
    @Qualifier("userCtrlAuto")  // 限定词, 指定id
    UserController userController;

    @Autowired   //自动装配注解
    @Qualifier("userBizAuto")
    UserBiz userBiz;

    @Autowired
    UserMapper userMapper;


    @Test
    public void test3(){

        System.out.println("userController = " + userController);
        System.out.println("userBiz = " + userBiz);
        System.out.println("userMapper = " + userMapper);

    }


}
